<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2019/11/14 11:31
    // +----------------------------------------------------------------------
    // | Description:
    // +----------------------------------------------------------------------
    namespace app\common\model;

    use think\Exception;
    use think\Model;
    use traits\controller\Jump;

    class BaseModel extends Model
    {

        use Jump;

        protected $update = ["update_time"];
        protected $insert = ["create_time", "ip"];



        protected function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub
        }


        /**
         * @return int
         * @Author: LuckyHhy <jackhhy520@qq.com>
         * @name: setUpdateTimeAttr
         * @describe:
         */
        protected function setUpdateTimeAttr()
        {
            return time();
        }

        /**
         * @return mixed
         * @author: hhygyl
         * @name: setIpAttr
         * @describe:
         */
        protected function setIpAttr()
        {
            return request()->ip();
        }


        /**
         * @return int
         * @author: hhygyl
         * @name: setCreateTimeAttr
         * @describe:
         */
        public function setCreateTimeAttr()
        {
            return time();
        }


        /**
         * @return array
         * @throws \think\db\exception\BindParamException
         * @throws \think\exception\PDOException
         * @Author: LuckyHhy <jackhhy520@qq.com>
         * @name: getTablesList
         * @describe: 全部表
         */
        public function getTablesList()
        {
            $tables   = [];
            $database = strtolower(config('database.database'));
            $sql      = 'SHOW TABLES';
            $data     = self::query($sql);

            foreach ($data as $v) {
                $tables[] = $v["Tables_in_{$database}"];
            }

            return $tables;
        }

        /**
         * @param $table
         * @return bool
         * @throws \think\db\exception\BindParamException
         * @throws \think\exception\PDOException
         * @Author: LuckyHhy <jackhhy520@qq.com>
         * @name: tableExists
         * @describe:检查表是否存在
         */
        public function tableExists($table)
        {
            $tables = self::getTablesList();

            if (strpos($table, DB_PREFIX) === false) {
                $table = DB_PREFIX.$table;
            }

            return in_array($table, $tables) ? true : false;
        }


        /**
         * @param $table
         * @return mixed
         * @throws \think\db\exception\BindParamException
         * @throws \think\exception\PDOException
         * @Author: LuckyHhy <jackhhy520@qq.com>
         * @name: dropTable
         * @describe:刪除数据表
         */
        public function dropTable($table)
        {
            if (strpos($table, DB_PREFIX) === false) {
                $table = DB_PREFIX.$table;
            }

            return self::query("DROP TABLE {$table}");
        }


        /**
         * @param $table
         * @return array
         * @throws \think\db\exception\BindParamException
         * @throws \think\exception\PDOException
         * @Author: LuckyHhy <jackhhy520@qq.com>
         * @name: getFieldsList
         * @describe:获取数据表字段
         */
        public function getFieldsList($table)
        {
            if (strpos($table, DB_PREFIX) === false) {
                $table = DB_PREFIX.$table;
            }
            $fields = [];
            $data   = self::query("SHOW COLUMNS FROM {$table}");
            foreach ($data as $v) {
                $fields[$v['Field']] = $v['Type'];
            }

            return $fields;
        }

        /**
         * @param $table
         * @param $field
         * @return bool
         * @throws \think\db\exception\BindParamException
         * @throws \think\exception\PDOException
         * @Author: LuckyHhy <jackhhy520@qq.com>
         * @name: fieldExists
         * @describe:检查字段是否存在
         */
        public function fieldExists($table, $field)
        {
            $fields = self::getFieldsList($table);

            return array_key_exists($field, $fields);
        }


        /**
         * @param $table
         * @return string
         * @Author: LuckyHhy <jackhhy520@qq.com>
         * @name: getTableName
         * @describe:获取表名
         */
        public function getTableName($table)
        {
            if (strpos($table, DB_PREFIX) === false) {
                $table = DB_PREFIX.$table;
            }

            return $table;
        }

        /**
         * @param array $data
         * @param bool $return_id
         * @param bool $sql_print
         * @return int|string
         * @Author: LuckyHhy <jackhhy520@qq.com>
         * @name: JrkInsert
         * @describe: 数据插入操作
         */
        public function JrkInsert($data = [], $sql_print = false)
        {

            // 返回影响数据的条数，没修改任何数据返回 0
            $result = self::isUpdate(false)->allowField(true)->save($data);

            // 打印SQL
            if ($sql_print) {
                self::getLastSql();
            }

            return $result;
        }


        /**
         * @param array $data
         * @param null $where
         * @param bool $sql_print
         * @return bool|int|string
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @Author: LuckyHhy <jackhhy520@qq.com>
         * @name: JrkUpdate
         * @describe:更新数据操作
         */
        public function JrkUpdate($data = [], $where = null, $sql_print = false)
        {
            if (empty($where)) {
                $result = self::isUpdate(true)->allowField(true)->save($data);

            } else {
                $result = self::where($where)->update($data);
            }

            // 打印SQL
            if ($sql_print) {
                self::getLastSql();
            }

            return $result;
        }


        /**
         * @param $id
         * @param bool $sql_print
         * @return bool
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/2/25
         * @name: JrkDelete
         * @describe:删除
         */
        public function JrkDelete($id, $sql_print = false)
        {
            try {
                if (is_array($id)){
                    $ids=$id;
                }else{
                    $ids=@explode(",",$id);
                }
                $result = self::where('id', 'in',$ids)->delete();
                // 打印SQL
                if ($sql_print) {
                    self::getLastSql();
                }
                if ($result) {
                    $this->success("删除成功");
                } else {
                    $this->error("删除失败");
                }
            } catch (Exception $exception) {

                $this->error($exception->getMessage());
            }

        }


        /**
         * @param $data
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/13
         * @name: doAll
         * @describe:编辑新增
         */
        public function doAll($data)
        {
            try {
                if (isset($data['id'])) {
                    $result = $this->JrkUpdate($data);
                } else {
                    $result = $this->JrkInsert($data);
                }
                if ($result) {
                    $this->success("操作成功");
                } else {
                    $this->error("操作失败");
                }
            } catch (Exception $exception) {
                $this->error($exception->getMessage());
            }
        }



    }
